package exttable.testselector;

import dbutils.DbUtils;
import dbutils.DeleteProbationer;
import exttable.StatisticPanel;
import exttable.analysiscreation.CreateNewAnalysis;
import exttable.dbchecking.RunDBChecking;
import exttable.dbclasses.DicGeneral;
import exttable.formgenerator.*;
import exttable.userdefineextendsclasses.UserDefineJMenu;
import exttable.userdefineextendsclasses.UserDefineJMenuItem;
import exttable.testversioning.testversionrunner.TestVersionMVC;
import exttable.timetable.TimeTableMainPanel;
import exttable.timetable.groupshchemapanel.GroupsSchemaFrame;
import exttable.timetable.groupshchemapanel.ViewGroupsInSchema;
import options.SettingsFrame;
import utils.Localisation;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jan 12, 2010
 * Time: 12:56:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class VolunteerSelectorView {
    public static final int FRAME_HEIGHT = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 150;
    public static final int FRAME_WIDTH = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - 200;
    public static final int CELL_WIDTH = 200;
    public static final int AMOUNT_OF_COLUNS_OF_AUTO_RESIZE = 14;
    public static final String ADD_VIEW_RESULT = "addviewresults";
    public static final String CREATE_TIME_TABLE = "createtimetable";
    public static final String VERSION_CONSTRUCTOR = "versionconstructor";
    public static final String FILL_SCHEMA_WITH_BATCHES = "fillschemawithbatches";

    private JList typeList;
    private JList groupList;
    private JList schemaList;
    private JList probationList;
    private JList phasesList;
    private JTable timeTable = new JTable();
    private JScrollPane timeTableScroll;
    private JMenuBar menuBar;
    private JPanel menuPanel = new JPanel();
    private Box selectorBox;
    private JLabel infoLabel;
    private SpringLayout layout = new SpringLayout();

    private JFrame frame;
    private VolunteerSelectorModel model;
    private static Map<java.util.List<String>, java.util.List<DicGeneral>> mapDicGeneral=StaticMethods.getMapFromDic();

    public static Map<java.util.List<String>, java.util.List<DicGeneral>> getMapDicGeneral() {
        return mapDicGeneral;
    }

    public VolunteerSelectorView(VolunteerSelectorModel model) throws ClassNotFoundException, SQLException {
        this.model = model;
        init();
    }

    public void init() throws ClassNotFoundException, SQLException {
        frame = new JFrame(Localisation.getMessage("title.timetable")+" "+DbUtils.ModeOfOperation);
        frame.setLayout(layout);
        menuPanel = new JPanel();
        menuPanel.setLayout(new GridLayout(1, 1));
        menuBar = new JMenuBar();
        selectorBox = Box.createHorizontalBox();
        infoLabel = new JLabel();
        infoLabel.setForeground(Color.GRAY);
        typeList = new JList(model.getListTypes());
        groupList = new JList(model.getListGroups());
        schemaList = new JList(model.getListSchemas());
        probationList = new JList(model.getListProbationer());
        phasesList = new JList(model.getListPhases());

        groupList.setFixedCellWidth(CELL_WIDTH);
        probationList.setFixedCellWidth(CELL_WIDTH);
        typeList.setFixedCellWidth(CELL_WIDTH);
        phasesList.setFixedCellWidth(CELL_WIDTH);

        groupList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        probationList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        typeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        phasesList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        timeTableScroll = new JScrollPane();
        UserDefineJMenu testMenu = new UserDefineJMenu(Localisation.getMessage("menu.test"),2);
        UserDefineJMenuItem subAddViewResults = new UserDefineJMenuItem(Localisation.getMessage("menu.addviewresults"),2);
        subAddViewResults.setActionCommand(ADD_VIEW_RESULT);
        testMenu.add(subAddViewResults);

        UserDefineJMenu timetableMenu = new UserDefineJMenu(Localisation.getMessage("menu.timetable"),1);
        UserDefineJMenuItem createTimeTable = new UserDefineJMenuItem(Localisation.getMessage("menu.createtimetable"),4);
        UserDefineJMenu groupBatch = new UserDefineJMenu(Localisation.getMessage("menu.groupschema"),1);
        UserDefineJMenuItem fillSchemaWithGroups = new UserDefineJMenuItem(Localisation.getMessage("menuItem.fillshemawithgroups"),4);
        UserDefineJMenuItem viewFilledSchemas = new UserDefineJMenuItem(Localisation.getMessage("menuItem.viewfilledschemas"),1);
        fillSchemaWithGroups.setActionCommand(FILL_SCHEMA_WITH_BATCHES);
        createTimeTable.setActionCommand(CREATE_TIME_TABLE);
        groupBatch.add(fillSchemaWithGroups);
        groupBatch.add(viewFilledSchemas);

        UserDefineJMenu statMenu = new UserDefineJMenu(Localisation.getMessage("menu.statistic"),1);
        UserDefineJMenuItem analysData = new UserDefineJMenuItem(Localisation.getMessage("menu.analysdata"),4);
        statMenu.add(analysData);

        UserDefineJMenu addViewMenu = new UserDefineJMenu(Localisation.getMessage("menu.tuning"),1);

        UserDefineJMenu volunteerMenu = new UserDefineJMenu(Localisation.getMessage("menu.volunteer"),1);
         if(DbUtils.GENERAL_DATABASE_NAME.equals(DbUtils.SECONDARY_DATABASE_NAME+"_copy")){
          volunteerMenu.setEnabled(false);
        }
        UserDefineJMenuItem addVolunteer = new UserDefineJMenuItem(Localisation.getMessage("menuItem.add"),2);
        addVolunteer.setName("volunteers");
        addVolunteer.addActionListener(ListenerFactory.getFormGenerator(null));

        UserDefineJMenuItem addAdditionalDataForVolunteers = new UserDefineJMenuItem(Localisation.getMessage("menuItem.addAdditionalDataForVolunteers"),2);
        addAdditionalDataForVolunteers.setName("additional_data_for_volunteers");
        addAdditionalDataForVolunteers.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FormAddAdditionalDataForVolunteers();
            }
        });

        UserDefineJMenuItem viewVolunteer = new UserDefineJMenuItem(Localisation.getMessage("menuItem.view"),1);
        viewVolunteer.setName("volunteers");
        viewVolunteer.addActionListener(ListenerFactory.getViewGenerator());
        UserDefineJMenuItem editVolunteer = new UserDefineJMenuItem(Localisation.getMessage("error.edit"),3);
        editVolunteer.setName("volunteers");
        editVolunteer.addActionListener(ListenerFactory.getComboBoxSelector(StaticMethods.getVolunteerList().toArray()));
        volunteerMenu.add(addVolunteer);
        volunteerMenu.add(addAdditionalDataForVolunteers);
        volunteerMenu.add(viewVolunteer);
        volunteerMenu.add(editVolunteer);

        UserDefineJMenu probationerMenu = new UserDefineJMenu(Localisation.getMessage("menu.probationer"),2);
        UserDefineJMenuItem addProbationer = new UserDefineJMenuItem(Localisation.getMessage("menuItem.add"),4);
        addProbationer.setName("probationers");
//        addProbationer.addActionListener(ListenerFactory.getFormGenerator(null));
        addProbationer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FormAddProbationer();
            }
        });
        UserDefineJMenuItem viewProbationer = new UserDefineJMenuItem(Localisation.getMessage("menuItem.view"),2);
        viewProbationer.setName("probationers");
        viewProbationer.addActionListener(ListenerFactory.getFilterSelector());

        UserDefineJMenuItem editProbationer = new UserDefineJMenuItem(Localisation.getMessage("error.edit"),4);
        editProbationer.setName("probationers");
        editProbationer.addActionListener(ListenerFactory.getComboBoxSelector(StaticMethods.getProbationerList().toArray()));

        UserDefineJMenuItem deleteProbationer = new UserDefineJMenuItem(Localisation.getMessage("labelRemove"),4);
        deleteProbationer.setName("probationers");
        deleteProbationer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new DeleteProbationer();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        probationerMenu.add(addProbationer);
        probationerMenu.add(viewProbationer);
        probationerMenu.add(editProbationer);
        probationerMenu.add(deleteProbationer);
        UserDefineJMenu groupMenu = new UserDefineJMenu(Localisation.getMessage("menu.group"),4);
        UserDefineJMenuItem addGroup = new UserDefineJMenuItem(Localisation.getMessage("menuItem.add"),4);
        addGroup.setName("groups");
        addGroup.addActionListener(ListenerFactory.getFormGenerator(null));
        UserDefineJMenuItem viewGroup = new UserDefineJMenuItem(Localisation.getMessage("menuItem.view"),4);
        viewGroup.setName("groups");
        viewGroup.addActionListener(ListenerFactory.getViewGenerator());
        UserDefineJMenuItem editGroup = new UserDefineJMenuItem(Localisation.getMessage("error.edit"),4);
        editGroup.setName("groups");
        editGroup.addActionListener(ListenerFactory.getComboBoxSelector(StaticMethods.getGroupList().toArray()));
        groupMenu.add(addGroup);
        groupMenu.add(viewGroup);
        groupMenu.add(editGroup);

        UserDefineJMenu questionnaireMenu = new UserDefineJMenu(Localisation.getMessage("menu.questionnaire"),1);

        UserDefineJMenuItem fillQuestionnaire = new UserDefineJMenuItem((Localisation.getMessage("timetable.fill")),2);
        fillQuestionnaire.addActionListener(ListenerFactory.genenerateSelectPhaseAndProbationer("new"));

        UserDefineJMenuItem editQuestionnaire = new UserDefineJMenuItem((Localisation.getMessage("error.edit")),2);
        editQuestionnaire.addActionListener(ListenerFactory.genenerateSelectPhaseAndProbationer("edit"));

        UserDefineJMenuItem downloadResults = new UserDefineJMenuItem((Localisation.getMessage("form.saveresult")),4);
        downloadResults.addActionListener(ListenerFactory.RunQuestionnaireAnalyser());
        questionnaireMenu.add(fillQuestionnaire);
        questionnaireMenu.add(editQuestionnaire);
        questionnaireMenu.add(downloadResults);

        UserDefineJMenuItem versionConstruction = new UserDefineJMenuItem(Localisation.getMessage("menuItem.versionconstruction"),4);
        versionConstruction.setActionCommand(VERSION_CONSTRUCTOR);

        UserDefineJMenuItem dump = new UserDefineJMenuItem(Localisation.getMessage("getDumpZip"),3);
        dump.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                StaticMethods.getMysqlDumpZip();
            }
        });

        UserDefineJMenuItem createNewAnalysis=new UserDefineJMenuItem(Localisation.getMessage("labelNewAnalysis"),3);
        createNewAnalysis.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CreateNewAnalysis();
            }
        });

        UserDefineJMenuItem checkDBForWarnings=new UserDefineJMenuItem(Localisation.getMessage("checkDBForWarnings"),4);
        checkDBForWarnings.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RunDBChecking();
            }
        });

        addViewMenu.add(volunteerMenu);
        addViewMenu.add(probationerMenu);
        addViewMenu.add(groupMenu);
        addViewMenu.add(questionnaireMenu);
        addViewMenu.add(ComponentsToPanel.createDicMenu());
        addViewMenu.add(versionConstruction);
        addViewMenu.add(dump);
        addViewMenu.add(createNewAnalysis);
        addViewMenu.add(checkDBForWarnings);
        addViewMenu.add(createOptionsMenuItem("options"));

        createTimeTable.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new TimeTableMainPanel();
            }
        });

        viewFilledSchemas.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ViewGroupsInSchema();

            }
        });

        fillSchemaWithGroups.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                new FillGroupsInSchema();
                new GroupsSchemaFrame();
            }
        });
        subAddViewResults.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    generateSelectorPanels();
                    organiseSelectorLayout();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        analysData.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new StatisticPanel();
            }
        });

        versionConstruction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new TestVersionMVC();
            }
        });


        timetableMenu.add(createTimeTable);
        timetableMenu.add(groupBatch);
        menuBar.add(testMenu);
        menuBar.add(timetableMenu);
        menuBar.add(statMenu);
        menuBar.add(addViewMenu);
        menuPanel.add(menuBar);
        organiseMenuLayout();
        frame.add(menuPanel);
        frame.add(selectorBox);
        frame.add(timeTableScroll);
        frame.add(infoLabel);
        frame.setVisible(true);
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(ListenerFactory.closeFrame());
//        UserDefineJMenu.lookOverJMenuBar(menuBar);
    }

    private void generateSelectorPanels() {
        try {
            selectorBox.add(new JScrollPane(typeList));
//            selectorBox.add(new JScrollPane(groupList));
            selectorBox.add(new JScrollPane(phasesList));
            selectorBox.add(new JScrollPane(probationList));
            selectorBox.add(new JScrollPane(schemaList));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void organiseMenuLayout() {
        layout.putConstraint(SpringLayout.WEST, menuBar, 0, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, menuBar, 0, SpringLayout.NORTH, frame);
    }

    public void organiseSelectorLayout() {
        layout.putConstraint(SpringLayout.WEST, selectorBox, 0, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, selectorBox, 0, SpringLayout.SOUTH, menuBar);
        layout.putConstraint(SpringLayout.WEST, infoLabel, 0, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, infoLabel, 0, SpringLayout.SOUTH, selectorBox);
        frame.getContentPane().repaint();
        frame.getContentPane().validate();
    }

    public void organiseResultsLayout() {
        timeTableScroll.setPreferredSize(new Dimension(FRAME_WIDTH - 25, 400));
        layout.putConstraint(SpringLayout.WEST, timeTableScroll, 0, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, timeTableScroll, 0, SpringLayout.SOUTH, infoLabel);
        frame.getContentPane().repaint();
        frame.getContentPane().validate();
    }

    public JList getTypeList() {
        return typeList;
    }

    public JList getGroupList() {
        return groupList;
    }

    public JLabel getInfoLabel() {
        return infoLabel;
    }

    public JList getProbationList() {
        return probationList;
    }

    public JList getSchemaList() {
        return schemaList;
    }

    public JList getPhasesList() {
        return phasesList;
    }

    public JTable getTimeTable() {
        return timeTable;
    }

    public JScrollPane getTimeTableScroll() {
        return timeTableScroll;
    }

    public void addTypeListAction(ListSelectionListener action) {
        typeList.addListSelectionListener(action);
    }

    public void addGroupListAction(ListSelectionListener action) {
        groupList.addListSelectionListener(action);
    }

    public void addProbationListAction(ListSelectionListener action) {
        probationList.addListSelectionListener(action);
    }

    public void addSchemaListAction(ListSelectionListener action){
        schemaList.addListSelectionListener(action);
    }

    public void addPhasesList(ListSelectionListener action){
        phasesList.addListSelectionListener(action);
    }

    public JFrame getFrame() {
        return frame;
    }

    private JMenu createOptionsMenuItem(String name) {
        final JMenu jMenu = new JMenu(Localisation.getMessage(name));
        jMenu.setName(name);
        JMenuItem settingsJMenuItem=new JMenuItem(Localisation.getMessage("settings"));
        settingsJMenuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                SettingsFrame settingsFrame=new SettingsFrame();
                settingsFrame.setLogIn(true);
            }
        });
        jMenu.add(settingsJMenuItem);
        return jMenu;
    }
}
